<?php
/*
*角色接口类
*/
class roleAppClass extends appBase{
	/*行政人事-角色管理-权限导航*/
	public function search(){
		$json['power'][] = array("name" => "全部","array" => array());
		foreach($this->powerAll as $oneKey => $oneValue){
			$two = array(
				array("name" => "全部","key" => ""),
			);
			foreach($oneValue as $twoKey => $twoValue){
				$two[] = array(
					"key" => $twoKey,//二级菜单关键词
					"name" => $twoValue['name'],//二级菜单名称
				);
			}
			$json['power'][] = array(
				"name" => $oneKey,//一级菜单名称
				"array" => $two,//二级菜单数组
			);
		}
		$json['warn'] = "success";
		return $json;
	}
	/*行政人事-角色管理-列表页*/
	public function home(){
		if(!$this->power("role","see")){
			$json['warn'] = "您没有查看角色档案的权限";
		}else{
			//like查询数组
			$like = array(
				"name" => $this->post['name'],//角色名称
			);
			//权限查询
			$powerOne = $this->post['powerOne'];//一级导航
			$powerTwo = $this->post['powerTwo'];//二级导航
			if(!empty($powerOne)){
				if(empty($powerTwo)){
					$and = "";
					foreach($this->powerAll[$powerOne] as $key => $value){
						if(!empty($and)){
							$or = " or ";
						}
						$and .= $or." power like '%\"$key\":%' ";
					}
					$powerSearch = " and ( ".$and." ) ";
				}else{
					$powerSearch = " and power like '%\"$powerTwo\":%' ";
				}
			}
			//获取查询结果
			$where = search(array("like" => $like)).$powerSearch;
			//数据查询
			$json['role'] = array();
			$page = $this->page(array("table" => "role","where" => $where,"order" => "list"));
			foreach($page['array'] as $array){
				//权限
				$power = array();
				$powerNow = json_decode($array['power'],true);
				if(empty($powerOne)){
					foreach($this->powerAll as $menuOneName => $menuOneValue){
						if(count(array_intersect_key($menuOneValue,(array)$powerNow)) > 0){
							$power[] = array("name" => $menuOneName);
						}
					}
				}else{
					if(empty($powerTwo)){
						foreach($this->powerAll[$powerOne] as $menuTwoName => $menuTwoValue){
							if(count(array_intersect(array_keys($menuTwoValue),(array)$powerNow[$menuTwoName])) > 0){
								$power[] = array("name" => $menuTwoValue['name']);
							}
						}
					}else{
						foreach($this->powerAll[$powerOne][$powerTwo] as $key => $value){
							if(in_array($key,(array)$powerNow[$powerTwo])){
								$power[] = array("name" => $value);
							}	
						}
					}
				}
				//角色信息
				$json['role'][] = array(
					"id" => $array['id'],//角色ID
					"name" => $array['name'],//名称
					"power" => $power,//权限
					"text" => $array['text'],//备注
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*行政人事-角色管理-详情页*/
	public function detail(){
		$role = query("role","id = '".$this->post['id']."'");
		if(!$this->power("role","see")){
			$json['warn'] = "您没有查看角色档案的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "角色ID为空";
		}elseif(empty($role['id'])){
			$json['warn'] = "未找到此角色";
		}else{
			//权限
			$powerNow = json_decode($role['power'],true);
			$power = array();
			foreach($this->powerAll as $oneKey => $oneValue){
				$menuOne = array(
					"menuOne" => $oneKey,//一级菜单名称
					"array" => array(),//二级菜单数组
				);
				foreach($oneValue as $twoKey => $twoValue){
					$menuTwo = array(
						"key" => $twoKey,//二级菜单关键词
						"menuTwo" => $twoValue['name'],//二级菜单名称
						"array" => array(),//权限数组
					);
					unset($twoValue['name']);
					unset($twoValue['cla']);
					unset($twoValue['fun']);
					foreach($twoValue as $key => $value){
						if(in_array($key,(array)$powerNow[$twoKey])){
							$hook = "yes";
						}else{
							$hook = "no";
						}
						$menuTwo['array'][] = array(
							"key" => $key,//权限关键词
							"name" => $value,//权限名称
							"hook" => $hook,//是否勾选
						);
					}
					$menuOne['array'][] = $menuTwo;
				}
				$power[] = $menuOne;
			}
			$json['role'] = array(
				"name" => $role['name'],//角色名称
				"text" => $role['text'],//备注
				"power" => $power,//权限
			);
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
}
?>